<?php


namespace app\api\controller;


use app\Validate\MemberShipValidate;
use think\Controller;
use data\service\Member as MemberService;

/**
 *
 * @author Administrator
 *
 */
class MemberShip extends Controller
{
    /**
     * 分享注册
     *
     * @return \multitype
     * @author dzl
     */
    public function memberRegister(MemberShipValidate $memberShipValidate)
    {
        $member = new MemberService();

        $share_id = $memberShipValidate->request->post('share_id');
        $user_name = $memberShipValidate->request->post('username');
        $nick_name = $memberShipValidate->request->post('nickname');
        $password = $memberShipValidate->request->post('password');
        $member_level = $memberShipValidate->post('level_name');
        $mobile = $memberShipValidate->request->post('tel');
        $email = request()->post('email', '');
        $sex = request()->post('sex', '');
        $status = $memberShipValidate->request->post('status');
        $shareUser = $member->getMemberInfo($share_id);
        $parent_id = $shareUser['parent_id'];

        $retval = $member->addMember($user_name, $password, $email, $sex, $status, $mobile, $member_level, $parent_id, $share_id);
        $result = $member->updateNickNameByUid($retval, $nick_name);
        return AjaxReturn($retval);

    }

    /**
     * 分享次数
     *
     * @return string
     * @author dzl
     */
    public function memberShare(MemberShipValidate $memberShipValidate)
    {
        $member = new \data\service\Member();
        $id = $memberShipValidate->request->post('id');
        $info = $member->getMemberInfo($id);
        $num = $info['sharing_times'];
        if ($num > 0) {
            return json_encode(ApiReturn(SUCCESS, ['uid' => $id]));

        }
        return json_encode(ApiReturn(SHARING_TIMES_EXHAUSTED));
    }

    /**
     * 会员收货地址添加
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function addressInsert(MemberShipValidate $memberShipValidate)
    {
//        if (request()->isAjax()) {
        $member = new MemberService();
        $id = $memberShipValidate->request->post('id');
        $consigner = $memberShipValidate->request->post('consigner');
        $mobile = $memberShipValidate->request->post('mobile');
        $province = $memberShipValidate->request->post('province');
        $city = $memberShipValidate->request->post('city');
        $district = $memberShipValidate->request->post('district');
        $address = $memberShipValidate->request->post('address');
        $is_default = request()->post('is_default', 0);

        $retval = $member->addMemberAddress($id, $consigner, $mobile, $province, $city, $district, $address, $is_default);
        return return_json_data($retval);
//        }
    }

    /**
     * 设置默认收货地址
     *
     * @param MemberShipValidate $memberShipValidate
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function updateAddressDefault(MemberShipValidate $memberShipValidate)
    {
        $member = new MemberService();
        $id = $memberShipValidate->request->post('id');
        $uid = $memberShipValidate->request->post('uid');

        $retval = $member->newUpdateAddressDefault($id, $uid);
        return return_json_data($retval);
    }

    /**
     * 修改地址
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function updateMemberAddress()
    {
        $member = new MemberService();
        $id = request()->post('id');
        $consigner = request()->post('consigner');
        $mobile = request()->post('mobile');
        $phone = request()->post('phone');
        $province = request()->post('province');
        $city = request()->post('city');
        $district = request()->post('district');
        $address = request()->post('address');
        $zip_code = request()->post('zip_code');
        $alias = request()->post('alias');


        $retval = $member->newUpdateMemberExpressAddress($id, $consigner, $mobile, $phone, $province, $city, $district, $address, $zip_code, $alias);
        return return_json_data($retval);
    }

    /**
     * 删除地址
     *
     * @param MemberShipValidate $memberShipValidate
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function deleteMemberAddress(MemberShipValidate $memberShipValidate)
    {
        $member = new MemberService();
        $id = $memberShipValidate->request->post('id');
        $uid = $memberShipValidate->request->post('uid');

        $res = $member->deleteMemberAddress($id, $uid);
        return return_json_data($res);
    }

    /**
     * 获取收货地址
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function addressList(MemberShipValidate $memberShipValidate)
    {
        $member = new MemberService();
        $id = $memberShipValidate->request->post('id');
        $page_index = request()->post('page', '1');
        $addresslist = $member->getApiMemberExpressAddressList($id, 1, 5, '', '');
        return return_json_data($addresslist);
    }


    /**
     * 获取地址详情
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function getAddressInfo(){
        $member = new MemberService();
        $id = request()->post('id');
        $addresslist = $member->getApiMemberExpressAddressInfo($id);
        return return_json_data($addresslist);
    }

    public function userInfo()
    {
        $member = new MemberService();
        $id = request()->get('id');
        $addresslist = $member->getInfo($id);
//        var_dump($addresslist);die;
        return return_json_data($addresslist);
    }
}
